package be.jabapage.batch;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;


import be.jabapage.db.general.Address;
import be.jabapage.db.snooker.Bond;
import be.jabapage.db.snooker.Club;
import be.jabapage.util.HibernateUtil;

public class ReadXMLFiles {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		readClubs("./xml/clubs.xml");

	}
	
	public static void readClubs(String path) {
		try {
			SAXBuilder builder = new SAXBuilder();
			Document myDocument = builder.build(new File(path));
			Element root = myDocument.getRootElement();
			List content = root.getChildren("club");
			Session mySession = HibernateUtil.getSessionFactory().openSession() ;
			Transaction myTransaction = mySession.beginTransaction();
			Query delBond = mySession.createQuery("delete from Bond") ;
			Query delClubs = mySession.createQuery("delete from Club");
			Query delPlayers= mySession.createQuery("delete from Player");
			delPlayers.executeUpdate();
			delClubs.executeUpdate();
			delBond.executeUpdate();
			
			Bond myBond = new Bond("VSF Oost Vlaanderen");
			mySession.save(myBond) ;
			for (Iterator iter = content.iterator(); iter.hasNext();) {
				Element current = (Element) iter.next();
				Element name= current.getChild("name");
				String strName = name.getValue();
				System.out.println(strName);
				Element number = current.getChild("number");
				int intNumber = Integer.parseInt(number.getValue()) ;
				Club currentClub = new Club(intNumber,strName);
				currentClub.setBond(myBond) ;
				Element address = current.getChild("address") ;
				String address1 = address.getChildText("address1");
				String address2 = address.getChildText("address2") ;
				String address3 = address.getChildText("address3");
				String zipCode = address.getChildText("zipcode") ;
				String city = address.getChildText("city") ;
				String telephone = address.getChildText("telephone") ;
				Address clubAddress = new Address();
				clubAddress.setAddress1(address1) ;
				clubAddress.setAddress2(address2) ;
				clubAddress.setAddress3(address3) ;
				clubAddress.setZipCode(zipCode) ;
				clubAddress.setCity(city) ;
				clubAddress.setTelephone(telephone);
				currentClub.setAddress(clubAddress);
				mySession.save(clubAddress) ;
				mySession.save(currentClub) ;
				
			}
			
			myTransaction.commit();
			
			Query qryClub = mySession.createQuery("select club from Club as club inner join be.jabapage.db.general.Address address where address.city = 'Gent'") ;
			qryClub.executeUpdate();
			for (Iterator iter = qryClub.iterate(); iter.hasNext(); ) {
				Club myClub = (Club) iter.next();
				System.out.println("club: " + myClub.getName()) ;
				Address myAddress = myClub.getAddress() ;
				System.out.println("  address: " + myAddress.getAddress1()) ;
			}
			
		} catch (JDOMException e) {
			System.err.println("Error processing readClubs : " + e) ;
		} catch (NullPointerException e) {
			e.printStackTrace();
			
		} catch (IOException e ) {
			System.err.println("file not found") ;
			System.err.println(e) ;
		}
	}

}
